import count from '@dfeidao/atom-nodejs/db/count';
import query from '@dfeidao/atom-nodejs/db/query';
// import remove_duplicate from '@feidao/web/atom/core/arr/remove-duplicate';
// import remove_duplicate from '@dfeidao/'
// import remove_duplicate from '@dfeidao/atom-web'
// import { IBaseTable } from '@dfeidao/web/atom/nodejs/db/interfaces';


interface Message {
	productid: string;
	page_size: number;
	client_type: string;
	page_no: number;
}

interface Data {
	formid: string;
	formname: string;
}

export default async function atom(msg: Message, action_id: string, session_id: string) {

	const systemid = '12';
	const { exec, prepare } = query(action_id, session_id, systemid);
	const productid = msg.productid;
	const client_type = msg.client_type;
	prepare('pub_menu', ['menu_no', 'menu_name', 'priority', 'systemid', 'formid', 'refsystemid', 'parent_no', 'show_type', 'background', 'is_show'], { productid, client_type }, msg.page_size, msg.page_no, ['menu_no'], []);
	const [data] = await exec<Data>();
	// console.log(data)
	const arr1 = new Array() as [{}];
	data.map((v) => {
		if (v.formid) {
			arr1.push(v.formid);
		}
	});
	function uniq(array: [{}]) {
		const temp = []; // 一个新的临时数组
		for (let i = 0; i < array.length; i++) {
			if (temp.indexOf(array[i]) === -1) {
				temp.push(array[i]);
			}
		}
		return temp;
	}
	const resultarr = uniq(arr1);
	// const resultarr = remove_duplicate(arr1);
	prepare('pub_form', ['formid', 'formname'], { formid: { $in: resultarr }, productid }, 100, 1, [], []);
	const [data1] = await exec<Data>();
	const dataArray = data.map((item) => {
		const formid1 = item.formid;
		const ck_data = data1.filter((d) => {
			return d.formid === formid1;
		});
		const arr = new Array();
		ck_data.forEach((_item) => {
			const formname = _item.formname;
			arr.push(formname);
			return;
		});
		item.formname = arr[0] ? arr[0] : '';
		return item;
	});
	// return dataArray;
	// console.log(dataArray)
	const c = count(action_id, session_id, systemid);
	c.prepare('pub_menu', { productid, client_type });
	const [t] = await c.exec();
	return { dataArray, count: t };
}
